package com.wyb.fastdfs.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wyb.fastdfs.service.DownloadService;
import com.wyb.fastdfs.service.UploadService;

/**
 * @Author:wangyibo
 * @Date 2021年8月10日
 * @Version:1.0
 * @Description:TODO
 */
@RestController
@RequestMapping("/file")
public class FastDfsFileController {

	@Autowired
	private UploadService uploadService;

	@Autowired
	private DownloadService downloadService;

	@PostMapping("/uploadImage")
	@ResponseBody
	public String uploadImage(MultipartFile file) throws IOException {
		String path = this.uploadService.uploadImage(file);
		return path;
	}

	@PostMapping("/uploadFile")
	@ResponseBody
	public String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
		String path = this.uploadService.uploadFile(file, request.getParameter("category"),
				request.getParameter("author"));
		return path;
	}

	@GetMapping("/downloadFile")
	@ResponseBody
	public void downloadFile(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
		downloadService.downloadFile(path, response);

	}
}
